import 'package:flutter_tts/flutter_tts.dart';

class TTSUtil {
  TTSUtil._();
  static final TTSUtil _manager = TTSUtil._();
  factory TTSUtil() {
    return _manager;
  }
  FlutterTts flutterTts = FlutterTts();

  initTTS() {
    setLanguage("zh-CN");
    setVolume(0.8);
    setSpeechRate(0.5);
    setPitch(1.0);
  }

  /// 设置语言
  Future setLanguage(String language) async {
    await flutterTts.setLanguage(language);
  }

  /// 设置音量
  Future setVolume(double volume) async {
    await flutterTts.setVolume(volume);
  }

  /// 设置语速
  Future setSpeechRate(double speechRate) async {
    await flutterTts.setSpeechRate(speechRate);
  }

  /// 设置音调
  Future setPitch(double pitch) async {
    await flutterTts.setPitch(pitch);
  }


  /// 说话
  Future speak(String text) async {
    if (text.isNotEmpty) {
      await flutterTts.speak(text);
    }
  }

  /// 暂停
  Future pause() async {
    await flutterTts.pause();
  }

  /// 结束
  Future stop() async {
    await flutterTts.stop();
  }
}